package com.ruoyi.web.controller.store.healthy;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.Healthy;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.HealthyMapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author：liyouxin
 * @date：2023/11/20 11:53
 */
@RestController
@RequestMapping("/store/healthy")
public class HealthyController extends BaseController {
	@Resource
	private HealthyMapper healthyMapper;

	@Log(title = "供应商导出", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Healthy healthy)
	{
		List<Healthy> healthies = healthyMapper.selectList(healthy);
		ExcelUtil<Healthy> util = new ExcelUtil<Healthy>(Healthy.class);
		util.exportExcel(response, healthies, "健康");
	}

	@Log(title = "查询健康证", businessType = BusinessType.OTHER)
	@GetMapping("/list")
	public TableDataInfo list(Healthy healthy)
	{
		startPage();
		List<Healthy> list = healthyMapper.selectList(healthy);
		return getDataTable(list);
	}


	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable Long id)
	{
		return success(healthyMapper.selectById(id));
	}

	@Log(title = "新增健康证", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public AjaxResult add(@Validated @RequestBody Healthy healthy)
	{
		healthy.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
		int result = healthyMapper.insert(healthy);
		if (result == 1) {
			return success();
		}
		return error();
	}

	@Log(title = "修改健康证", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody Healthy healthy) {
		healthy.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
		int result = healthyMapper.update(healthy);
		if (result == 1) {
			return success();
		}
		return error();
	}

	@Log(title = "删除人员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public AjaxResult remove(@PathVariable Long id)
	{
		return toAjax(healthyMapper.deleteById(id));
	}
	
}
